package com.yuyou.fn.settlement.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.settlement.service.IBalanceLockRecordService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.settlement.mapper.PosPayRecordMapper;
import com.yuyou.fn.settlement.entity.PosPayRecord;
import com.yuyou.fn.settlement.service.IPosPayRecordService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * PosPayRecord 表数据服务层接口实现类
 *
 */
@Service
public class PosPayRecordServiceImpl extends SuperServiceImpl<PosPayRecordMapper, PosPayRecord,Long> implements IPosPayRecordService {

    @Resource
    private IBalanceLockRecordService balanceLockRecordService ;

    @Override
    public boolean updateStatus(Long requestPayId, PosPayRecord.STATUS oldStatus, PosPayRecord.STATUS newStatus) {

        Example<PosPayRecord> example = PosPayRecord.createExample();
        example.createCriteria()
                .andEqualTo("requestPayId" , requestPayId )
                .andEqualTo("status" , oldStatus.ordinal() );
        PosPayRecord up = new PosPayRecord() ;
        up.setRequestPayId(requestPayId);
        up.setStatus(newStatus.ordinal());
        return updateSelectiveByExample(up,example );
    }

    @Override
    public boolean isPayingForOrder(Long busOrderId) {
        Example<PosPayRecord> example = PosPayRecord.createExample();
        example.createCriteria()
                .andEqualTo("busOrderId",busOrderId)
                .andEqualTo("status" , PosPayRecord.STATUS.已扫码.ordinal() )
                .andGreaterThanOrEqualTo("createTime", DateUtils.addMinutes(TimeUtils.now() , -5 ));
        return findOneByExample(example) != null ;
    }

    @Override
    public boolean isSuccessPay(Long requestPayId) {
        Example<PosPayRecord> example = PosPayRecord.createExample();
        example.createCriteria()
                .andEqualTo("requestPayId",requestPayId)
                .andEqualTo("status" , PosPayRecord.STATUS.已支付.ordinal() );
        return findOneByExample(example) != null ;
    }

    @Override
    public boolean updatePayStatus(Long requestPayId, JSONObject posInfo) {
        Example<PosPayRecord> example = PosPayRecord.createExample();
        example.createCriteria()
                .andEqualTo("requestPayId" , requestPayId )
                .andEqualTo("status" , PosPayRecord.STATUS.已扫码.ordinal() );
        PosPayRecord up = new PosPayRecord() ;
        up.setRequestPayId(requestPayId);
        up.setStatus(PosPayRecord.STATUS.已支付.ordinal());
        up.setPosInfo(posInfo != null ? posInfo.toJSONString() : null );
        return updateSelectiveByExample(up,example );
    }

    @Override
    public boolean isLockBalance(Long busOrderId) {
        Example<PosPayRecord> example = PosPayRecord.createExample();
        example.selectProperties("extra");
        example.createCriteria()
                .andEqualTo("busOrderId" , busOrderId )
                .andEqualTo("status" , PosPayRecord.STATUS.已扫码.ordinal() );

        List<PosPayRecord> list = findListByExample(example);

        for(PosPayRecord posPayRecord : list ){
            JSONObject extra = JSONObject.parseObject(StringUtils.defaultString(posPayRecord.getExtra(), "{}"));

            if( extra.containsKey("otherPays") ){
                JSONArray otherPays = extra.getJSONArray("otherPays");
                for(int i = 0 ; i < otherPays.size() ; i++ ){
                    JSONObject jsonObject = otherPays.getJSONObject(i);
                    if( jsonObject.getIntValue("payWay") == 3 ){
                        return true ;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public boolean cancel(Long busOrderId) {

        Example<PosPayRecord> example = PosPayRecord.createExample();
        example.createCriteria()
                .andEqualTo("busOrderId" , busOrderId )
                .andEqualTo("status" , PosPayRecord.STATUS.已扫码.ordinal() );

        List<PosPayRecord> list = findListByExample(example);

        list.stream().forEach(ppr -> {

            JSONObject extra = JSONObject.parseObject(StringUtils.defaultString(ppr.getExtra(), "{}"));

            if( extra.containsKey("otherPays") ){

                JSONArray otherPays = extra.getJSONArray("otherPays");
                for(int i = 0 ; i < otherPays.size() ; i++ ){
                    JSONObject jsonObject = otherPays.getJSONObject(i);
                    if( jsonObject.getIntValue("payWay") == 3 ){
                        balanceLockRecordService.unlock(extra.getLong("mainAccountId"), DigestUtils.md5Hex(ppr.getRequestPayId() + "pos") ,jsonObject.getBigDecimal("amount"));
                    }
                }
            }
            this.updateStatus(ppr.getRequestPayId(), PosPayRecord.STATUS.已扫码, PosPayRecord.STATUS.已撤销 );
        });

        return true ;
    }
}